package org.sd.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.sd.utils.ExcelChartUtils2;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.FileNotFoundException;
import java.util.List;
import java.util.Map;

@Controller
public class DashboardController {

    /**
     * 监控solr页面
     */
    @GetMapping("/monitor")
    public String solrMonitor(Model model) {
        model.addAttribute("message", "Hello, World!");
        return "index";
    }


    /**
     * 获取监控数据
     * @return
     */
    @PostMapping("/getData")
    @ResponseBody
    public JSONArray getData() throws FileNotFoundException {
        Map<String, List<String>> dataMap = ExcelChartUtils2.getData(ExcelChartUtils2.READ_FILE_PATH, 0, 0, 1);
        JSONArray arr = new JSONArray();
        JSONObject obj = new JSONObject();
        obj.put("key", dataMap.get("key"));
        obj.put("value", dataMap.get("value"));
        Map<java.lang.String, List<java.lang.String>> dataMap2 = ExcelChartUtils2.getData(ExcelChartUtils2.READ_FILE_PATH, 0, 0, 2);
        arr.clear();
        arr.add(obj);
        JSONObject obj2 = new JSONObject();
        obj2.put("key", dataMap2.get("key"));
        obj2.put("value", dataMap2.get("value"));
        arr.add(obj2);
        JSONArray array = new JSONArray();
        List<String> key = dataMap.get("key");
        List<String> value = dataMap.get("value");
        for (int i = 0; i < key.size(); i++) {
            JSONObject o = new JSONObject();
            o.put("name", key.get(i));
            o.put("value", value.get(i));
            array.add(o);
        }
        arr.add(array);
        return arr;
    }

}